var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goods_id : [],                                  // 用户选择的商品ID
        user_id:0,                                      // 委托人id
        goods_list:[],                                  // 用户选择的商品数据


        timeArray: ['1天', '3天', '7天', '30天'], 	 	 // 委托时间期限数据
        timeIndex: 0, 									// 当前选择时间
        phone:'',                                       // 联系方式
        ratio:'',                                       // 佣金比例
    },
    // 输入框输入
    inputEvent(e){
        var value = e.detail.value;
        var type = e.target.dataset.type;

        if (type == 'phone'){
            this.setData({phone:value})
        }
        if (type == 'ratio'){
            this.setData({ ratio:value})
        }
    },
	timesChange(e) {
		this.setData({
			timeIndex: e.detail.value,
		})
	},
    // 提交验证
    submitEntrustSell(){
        var self = this;
        var data = {};
        data.ids = this.data.goods_id;
        data.user_id = this.data.user_id;
        data.entrust_date = parseInt(this.data.timeArray[this.data.timeIndex]);
        data.phone = this.data.phone;
        data.ratio = this.data.ratio / 100;

        if (!data.entrust_date) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请选择委托时间',
            })
            return false;
        }
        if (!data.phone) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请输入联系方式',
            })
            return false;
        }
        if (!data.ratio) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请输入佣金比例',
            })
            return false;
        }
        // 验证成功 提交
        this.submit(data)
    },
    submit(data){
        var self = this;
        wx.getStorage({
            key: 'user',
            success: function (res) {
                data.openid = res.data.openid;
                data.token = res.data.token;
                wx.request({
                    url: app.url.requestUrl + '/entrust/setdateapply',
                    method: 'POST',
                    data: data,
                    success: function (res) {
                        var list = res.data;
                        if (res.data.code == 1) {
                            wx.showLoading({title: '申请成功'})
                            setTimeout(function(){
                                wx.hideLoading();
                                wx.redirectTo({
                                    url: '../trading-entrust/trading-entrust'
                                })
                            },1000)
                        }else{
                            wx.showToast({ image:"../../img/warning.png",title: list.msg })
                        }
                    }
                })
            }
        })
    },
    // 生命周期函数--监听页面加载
    onLoad: function(options) {
        var self = this;
        if (options.goods_id){
            wx.getStorage({
                key: 'user',
                success: function (res) {
                    wx.request({
                        url: app.url.requestUrl + '/entrust/setdate',
                        method: 'POST',
                        data: {
                            openid: res.data.openid,
                            token: res.data.token,
                            ids: options.goods_id,
                        },
                        success: function (res) {
                            var list = res.data;
                            if (res.data.code == 1) {
                                self.setData({
                                    goods_list: list.data
                                })
                            }
                        }
                    })
                }
            })
        }
        if (options.goods_id){
            this.setData({
                goods_id: options.goods_id
            })
        }else{
            wx.navigateBack({
                delta:1
            })
        }
        if (options.user_id){
            this.setData({
                user_id: options.user_id
            })
        }else{
            wx.navigateBack({
                delta: 2
            })
        }
    },
    bindShowMsg() {
        this.setData({
            select: !this.data.select
        })
    },
    mySelect(e) {
        var name = e.currentTarget.dataset.name
        this.setData({
            tihuoWay: name,
            select: false
        })
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})